#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#define PORT 2345   /*使用的PORT号码*/

#include <stdlib.h>
#include <stdio.h>
#include <strings.h>

int main(void)
{
     int sockfd, len;
     struct sockaddr_in addr;
     int addr_len=sizeof(struct sockaddr_in);
     char buffer[1024];

     /*建立socket*/
     if((sockfd = socket(AF_INET, SOCK_DGRAM, 0))<0) {
          perror("socket");
          exit(1);
     }
     
     /*填写sockaddr_in结构*/
     bzero(&addr, sizeof(addr));
    
     addr.sin_family=AF_INET;
     addr.sin_port=htons(PORT);
     addr.sin_addr.s_addr=htonl(INADDR_ANY);
    
     if(bind(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
           perror("connect");
           exit(1);
     }

     openlog("UDP-srv", LOG_PID|LOG_CONS, LOG_USER);
     while(1) {
             /*接收client端传来的字符串*/
			fd_set readfds;
			FD_ZERO(&readfds);
			FD_SET(sockfd, &readfds);
            syslog(LOG_INFO, "select Port:%d\n", PORT);
			if(select(sockfd+1,&readfds,NULL,NULL,NULL) > 0) {
             bzero(buffer, sizeof(buffer));
             len=recvfrom(sockfd, buffer, sizeof(buffer), 0 , (struct sockaddr *)&addr, &addr_len);
             
             /*显示client端的网络地址IP*/
             syslog(LOG_INFO, "######receive %dB from %s\n", len, inet_ntoa(addr.sin_addr));

             /*将字串返回给client端*/
			 buffer[0]++;
             sendto(sockfd, buffer, len, 0, (struct sockaddr *)&addr, addr_len);
			}

      }/*::while(1)*/
     
}/*::main()*/
